import { ElMessage } from 'element-plus';
import { api } from '.';
import qs from 'qs';
import type { Defect, PageBase, Parameters, Result, ResultListWithPage } from '@/index.d';

export const getListByConditionsWithPage = async <T extends PageBase>(ndeDefect: Defect) => {
  try {
    const res = await api({
      method: 'post',
      url: '/ndeReport/ndeDefect/getListByConditionsWithPage',
      data: qs.stringify(ndeDefect)
    });
    if (res.status === 200) {
      const resData: ResultListWithPage<T> = res.data;
      if (resData.status) {
        return resData.body;
      }
    } else {
      ElMessage.error(res.statusText);
    }
  } catch (error) {
    ElMessage.error('获取失败，错误信息是：' + error);
  }
};

export const getListByConditions = async () => {
  try {
    const res = await api({
      method: 'post',
      url: '/ndeReport/ndeDefect/getListByConditions',
      data: qs.stringify({})
    });
    if (res.status === 200) {
      const resData: Result<Array<Defect>> = res.data;
      if (resData.status) {
        return resData.body;
      }
    } else {
      ElMessage.error(res.statusText);
    }
  } catch (error) {
    ElMessage.error('获取失败，错误信息是：' + error);
  }
};

export const getSeachSelectOptions = async (parameter: Parameters) => {
  const res = await api({
    method: 'get',
    url: '/ndeReport/ndeDefect/getSeachSelectOptions',
    params: {
      parameter
    }
  });
  if (res.status === 200) {
    return res.data as Array<string>;
  } else {
    ElMessage.error(res.statusText);
  }
};
